//自定义js 
//公共配置
mini.parse();


//修改密码
function save() {

    var form = new mini.Form("#updatepass_window");
    form.validate();
    if (form.isValid() == false) return;

    //提交数据
    if (mini.get("newpass2").getValue() != mini.get("newpass").getValue()) {
        mini.alert("两次输入的密码必须一致");
        return false;
    }
    $.ajax({
        type: "POST",
        url: "/User/Changepass",
        data: { oldpass: mini.get("oldpass").getValue(), newpass: mini.get("newpass").getValue() },
        success: function (r) {
            if (r.resultCode == "200") {
                mini.alert("修改成功");
                mini.get("updatepass_window").hide();
                $.cookie('isresset', '0');
            } else {
                mini.alert(r.info);
            }
        }
    });
}
// 修改密码
function updatepass_window_show() {
    var updatepass_window = mini.get("updatepass_window");
    updatepass_window.show("center", "middle");
}

//关闭修改密码
function updatepass_window_hide() {
    var updatepass_window = mini.get("updatepass_window");
    updatepass_window.hide();
}
// 退出
function logout() {
    mini.confirm('您确定要退出本次登录吗?', '系统提示', function (action) {
        if (action == "ok") {
            location.href = "/Logout";
        }
    });

}
// 修改皮肤
function onSkinChange() {
    // css
    var newTheme = $("#selectSkin").val();
    $.cookie('sxtheme', newTheme);
    sx.changeTheme(newTheme);

}


function onissame(e) {
    if (e.value != mini.get("newpass").getValue()) {
        e.errorText = "两次输入的密码必须一致";
        e.isValid = false;
    }
}

function onoldpass(e) {
    $.ajax({
        type: "POST",
        url: "/User/VerifyPass",
        dataType: "json",
        data: { password: mini.get("oldpass").getValue() },
        success: function (r) {
            if (r.resultCode != "200") {
                mini.get("oldpass").setValue("");
                mini.alert("密码输入有误");
            }
        }
    });
}
$(function () {
    $("#selectSkin").val($("#admintheme").attr("_curtheme")); // 设置皮肤的初始值
});

$(document).ready(function () {
    //$("#tabs").tabs({
    //    onSelect: function (title, index) {
    //        //console.log("select tabs ：title：", title, "; index:", index);
    //    }
    //});
    // InitLeftMenu(); // 初始化左侧
    tabClose();
    tabCloseEven(); // 绑定右键菜单事件
});
function InitLeftMenu() {
    mini.get("menuList").load();
    return false;
} 
// 菜单选中
function onNodeSelect(e) { 
    var node = e.node;
    mini.get("menuList").expandPath(node);
    var isLeaf = e.isLeaf; 
    if (isLeaf) {
        showTab(node);
    } 
}

function showTab(node) {
    var tabs = mini.get("tabs");

    var id = "tab$" + node.id;
    var tab = tabs.getTab(id);
    if (!tab) {
        tab = {};
        tab._nodeid = node.id;
        tab.name = id;
        tab.title = node.text;
        tab.iconCls = node.iconCls;
        tab.url = node.url;
        tab.showCloseButton = true;  
        tabs.addTab(tab);
    }
    tabs.activeTab(tab);
}
 
function addTab(subtitle, url, icon) { 
    var tabs = mini.get("tabs");
    var tab = {}; 
    tab.name = subtitle;
    tab.title = subtitle;
    tab.iconCls = icon;
    tab.url = url;
    tab.showCloseButton = true;
    tabs.addTab(tab);
    tabs.activeTab(tab); 
}

function Tab(subtitle, url, icon) {
    $('#tabs').tabs('add', {
        title: subtitle,
        content: createFrame(url),
        closable: true,
        width: $('#mainPanle').width() - 10,
        height: $('#mainPanle').height() - 26,
        iconCls: icon
    });
    startPace();
}
function startPace() {
    $(".pace").removeClass("pace-inactive");
    Pace.start();
}
function createFrame(url) {
    var s = '<iframe name="mainFrame" scrolling="auto" frameborder="0"  src="' + url + '" style="width:100%;height:100%;"></iframe>';
    return s;
}

function tabClose() {
    /*双击关闭TAB选项卡*/
    $("#tabs").on("dblclick", ".tabs-inner", function () {
        $('#tabs').tabs('close', $(this).children("span").text());
    });
    $("#tabs").on("contextmenu", ".tabs-inner", function (e) {
        $('#mmTab').menu('show', {
            left: e.pageX,
            top: e.pageY
        });
        var subtitle = $(this).children("span").text();
        $('#mmTab').data("currtab", subtitle);
        return false;
    }); 
}
var currentTab = null;
var tabs = mini.get("tabs");
function onBeforeOpen(e) { 
    currentTab = tabs.getTabByEvent(e.htmlEvent);
    if (!currentTab ) {
        e.cancel = true;
    }
}
//绑定右键菜单事件
function tabCloseEven() {
    //关闭当前
    $('#mm-tabclose').click(function () {
        var ctab = tabs.getActiveTab(); 
        if (ctab.name == "home" ) {
            return;
        }
        tabs.removeTab(ctab);
    });
    //全部关闭
    $('#mm-tabcloseall').click(function () { 
        var but = [];
        but.push(tabs.getTab("home"));
        tabs.removeAll(but);
    });
    //关闭除当前之外的TAB
    $('#mm-tabcloseother').click(function () {
        var ctab = tabs.getActiveTab();
        var but = [ctab];
        but.push(tabs.getTab("home"));
        tabs.removeAll(but);
    });
    //关闭当前右侧的TAB
    $('#mm-tabcloseright').click(function () {
        var ctab = tabs.getActiveTab();
        var alltabs = tabs.getTabs();
        if (alltabs.length <2) {
            return;
        }
        var bremovetab = false;
        var needremovetab = [];
        for (var i = 1; i < alltabs.length; i++) {
            if (bremovetab) {
                needremovetab.push(alltabs[i]);
               
            } 
            if (!bremovetab && ctab.name == alltabs[i].name) {
                bremovetab = true;
            }
        }
        removeTabList(needremovetab);
        return false;
    });
   
    //关闭当前左侧的TAB
    $('#mm-tabcloseleft').click(function () {
        var ctab = tabs.getActiveTab();
        var alltabs = tabs.getTabs();
        if (alltabs.length < 2) {
            return;
        }
        var bremovetab = true;
        var needremovetab = [];
        for (var i = 1; i < alltabs.length; i++) {
            if (bremovetab) {
                needremovetab.push(alltabs[i]);
            }
            if (ctab.name == alltabs[i].name) {
                bremovetab = false;
                break;
            }
        }
        removeTabList(needremovetab);
        return false;
    }); 
    //退出
    $("#mm-exit").click(function () {
        mini.get("mmTab").hide();
    });
}  
// 删除tab列表
function removeTabList(tablist) {
    for (var i = 0; i < tablist.length; i++) {
        tabs.removeTab(tablist[i]);
    }
}
//判断浏览器是否支持html5本地存储
function localStorageSupport() {
    return (('localStorage' in window) && window['localStorage'] !== null);
}
 
$(function () {
     
    // 左侧导航分类选中样式
    //$(".panel-body .accordion-body>ul>li").on('click', function () {
    //    $(this).siblings().removeClass('super-accordion-selected');
    //    $(this).addClass('super-accordion-selected');

    //    //新增一个选项卡
    //    var tabUrl = $(this).data('url');
    //    var tabTitle = $(this).text();
    //    //tab是否存在
    //    if ($("#tt").tabs('exists', tabTitle)) {
    //        $("#tt").tabs('select', tabTitle);
    //    } else {
    //        var content = '<iframe scrolling="auto" frameborder="0"  src="' + tabUrl + '" style="width:100%;height:99%;"></iframe>';
    //        $('#tt').tabs('add', {
    //            title: tabTitle,
    //            //content: content,
    //            href: tabUrl,
    //            closable: true
    //        });
    //    }
    //});
       
}); 